import { getFollowedList } from '../../../api/user'

Page({
  data: {
    name: '',
    isFollowedEnd: false,
    followedList: [],
    followedQuery: {
      condition: {
        groupId: '',
        keyword: ''
      },
      pager: {
        page: 1,
        pageSize: 10
      }
    }
  },

  onLoad: function (options) {
    const _this = this
    this.setData({
      'followedQuery.condition.groupId': options.groupId,
      name: options.name
    })
    this.followedList()
  },

  followedList() {
    if (!this.data.isFollowedEnd) {
      getFollowedList(this.data.followedQuery).then(res => {
        if (res.records.length) {
          let list = this.data.followedList
          list = list.concat(res.records)
          this.setData({
            followedList: list
          })
        }
        if (res.records.length < 10) {
          this.setData({
            isFollowedEnd: true
          })
        }
      })
    }
  },

  getMoreFollowed() {
    const page = this.data.followedQuery.pager.page++
    this.setData({
      'followedQuery.pager.page': page
    })
    this.followedList()
  },

  searchFollowed(e) {
    this.setData({
      'followedQuery.condition.keyword': e.detail,
      'followedQuery.pager.page': 1,
      followedList: [],
      isFollowedEnd: false
    })
    this.followedList()
  }
})
